package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.ApplicationV2DO;
import com.example.makerspace.domain.UserCompetitionRelationShipDO;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.enums.ResultCodeEnum;
import com.example.makerspace.mapper.ApplicationV2Mapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class ApplicationV2DAO extends BaseDAO<ApplicationV2DO>{
    @Resource
    private ApplicationV2Mapper applicationV2Mapper;

    @PostConstruct
    public void init(){
        this.mapper = applicationV2Mapper;
    }

    public final static String USERNAME = "username";

    public ApplicationV2DO selectByUsername(String username){
        return applicationV2Mapper.selectOne(new QueryWrapper<ApplicationV2DO>().eq(USERNAME, username));
    }

    public List<ApplicationV2DO> selectAll(){
        return applicationV2Mapper.selectList(null);
    }

    public List<ApplicationV2DO> selectLatest(Integer num){
        return applicationV2Mapper.selectList(new QueryWrapper<ApplicationV2DO>().orderByDesc("create_time").last("limit " + num));
    }

    public  List<ApplicationV2DO>  selectIsEnroll(String username,Integer competitionParentId) {
        return  applicationV2Mapper.selectList(new QueryWrapper<ApplicationV2DO>().eq("competition_parent_id",competitionParentId ).eq("username",username));

    }
    public IPage<ApplicationV2DO> selectAllByPage(Pagination pagination) {
        QueryWrapper<ApplicationV2DO> queryWrapper = new QueryWrapper<>();
        return mapper.selectPage(new Page<>(pagination.getPageNum(), pagination.getSize()), queryWrapper);
    }

    public  IPage<ApplicationV2DO> selectUserByCondition(Pagination pagination ,String queryParam) {
        QueryWrapper<ApplicationV2DO> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username",queryParam);
        queryWrapper.orderByDesc("create_time");
        return mapper.selectPage(new Page<>(pagination.getPageNum(), pagination.getSize()), queryWrapper);
    }

    public List<ApplicationV2DO> selectAllByUsername(String phone) {
        return applicationV2Mapper.selectList(new QueryWrapper<ApplicationV2DO>().eq(USERNAME, phone));
    }

    public List<ApplicationV2DO> selectByAllUsername(String phoneNumber) {
        return applicationV2Mapper.selectList(new QueryWrapper<ApplicationV2DO>().eq(USERNAME, phoneNumber));
    }

    public void deleteByUsername(String username) {
        applicationV2Mapper.delete(new QueryWrapper<ApplicationV2DO>().eq("username",username));
    }

    public void deleteByUsernameAndId(String username, Integer competitionId, Integer competitionParentId) {
        applicationV2Mapper.delete(new QueryWrapper<ApplicationV2DO>().eq("username",username).eq("competition_parent_id",competitionParentId)
                .eq("competition_id",competitionId));

    }

    public List<ApplicationV2DO> selectIsApplication(String username, Integer competitionId) {
        return   applicationV2Mapper.selectList(new QueryWrapper<ApplicationV2DO>().eq("username", username)
                .eq("competition_id", competitionId));
    }

    public IPage<ApplicationV2DO> selectByComptitionId(Pagination pagination, List<Integer> collect) {
        QueryWrapper<ApplicationV2DO> wrapper = new QueryWrapper<>();
        wrapper.in("competition_id",collect);
        return mapper.selectPage(new Page<>(pagination.getPageNum(), pagination.getSize()), wrapper);
    }

    public IPage<ApplicationV2DO> selectUserByPage(Pagination pagination) {
        QueryWrapper<ApplicationV2DO> wrapper = new QueryWrapper<>();
        return mapper.selectPage(new Page<>(pagination.getPageNum(), pagination.getSize()), wrapper);
    }

    public List<ApplicationV2DO> selectHave(String username, Integer competitionId) {
        return  applicationV2Mapper.selectList(new QueryWrapper<ApplicationV2DO>().eq("competition_Id",competitionId ).eq("username",username));
    }

        public IPage<ApplicationV2DO> selectWaitRelation(Pagination pagination) {
            QueryWrapper<ApplicationV2DO> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status","审核中");
          return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
        }

    public IPage<ApplicationV2DO> selectPass(Pagination pagination) {
        QueryWrapper<ApplicationV2DO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","赛题报名成功");
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }

    public IPage<ApplicationV2DO> selectFail(Pagination pagination) {
        QueryWrapper<ApplicationV2DO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","赛题报名失败");
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }
}
